package com.zhenyi.xiaohuoche.admin.web;

import java.util.ArrayList;
import java.util.List;

import com.cjw.model.DBMap;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;

import cn.hutool.core.date.DateTime;

public class StatisticController extends XHCAdminController {

	public void saleStat() {
		String date = getPara("date", new DateTime().toString("yyyy-MM-dd"));
		String startTime = date + " 00:00:00";
		String endTime = date + " 23:59:59";
		String startTime2 = getPara("startTime", new DateTime().toString("yyyy-MM-dd 00:00:00"));
		String endTime2 = getPara("endTime", new DateTime().toString("yyyy-MM-dd 23:59:59"));

		Kv kv = Kv.create();
		kv.set("startTime", startTime);
		kv.set("endTime", endTime);
		SqlPara sqlPara = xhcDb.getSqlPara("XHCSaleStat", kv);
		List<Record> list = xhcDb.find(sqlPara);

		ArrayList<String> xAxis = new ArrayList<>();
		ArrayList<Integer> yAxis = new ArrayList<>();
		for (Record record : list) {
			String time = new DateTime(record.getDate("time")).toString("yyyy-MM-dd HH:mm:ss");
			xAxis.add(time);
			yAxis.add(record.getInt("count"));
		}
		DBMap map = new DBMap();
		map.put("xAxis", xAxis);
		map.put("yAxis", yAxis);

		Kv kv2 = Kv.create();
		kv2.set("startTime", startTime2);
		kv2.set("endTime", endTime2);
		SqlPara sqlPara2 = xhcDb.getSqlPara("XHCOrderCount", kv2);
		Record orderCountRecord = xhcDb.findFirst(sqlPara2);
		Long orderCount = orderCountRecord.getLong("count") != null ? orderCountRecord.getLong("count") : 0;
		Double totalFee = (orderCountRecord.getDouble("totalFee") != null ? orderCountRecord.getDouble("totalFee") : 0)
				/ 100F;
		map.put("orderCount", orderCount);
		map.put("totalFee", totalFee);

		SqlPara sqlPara3 = xhcDb.getSqlPara("XHCOrderTicketCount", kv2);
		Record ticketCountRecord = xhcDb.findFirst(sqlPara3);
		Long ticketCount = ticketCountRecord.getLong("count");
		map.put("ticketCount", ticketCount);

		setAttr("date", date);
		setAttr("startTime", startTime2);
		setAttr("endTime", endTime2);
		setInfoItem(JsonKit.toJson(map));
		render("/xhc/admin/statistic/saleStat.html");
	}

}
